package com.ysd.dao.Impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ysd.entity.Leansnews;
import com.ysd.util.DBManager;

public class XinWenAnLiDaoImpl {
	//首页新闻案例
	public ArrayList<Leansnews> getXinWenList(){
		ArrayList<Leansnews> xinwen=new ArrayList<Leansnews>();//创建List
		Connection conn=DBManager.getConnection();//获取链接
		Statement st=null;
		ResultSet rs=null;
		try {//正常情况
			st=conn.createStatement();//创建Statement，作用是执行SQL语句
			rs=st.executeQuery("select * from leansnews order by rand() limit 4");//SQL语句
			while(rs.next()) {
				int newsId=rs.getInt("newsId");
				String newsTitle=rs.getString("newsTitle");
				String newsContent=rs.getString("newsContent");
				Leansnews leansnews=new Leansnews(newsId,newsTitle,newsContent);
   			    xinwen.add(leansnews);
			}
			return xinwen;
		} catch (SQLException e) {//异常情况
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
}
